package com.raising.modules.sys.controller;

import com.google.common.collect.Lists;
import com.raising.framework.controller.BaseController;
import com.raising.modules.sys.entity.Resource;
import com.raising.modules.sys.entity.Role;
import com.raising.modules.sys.entity.User;
import com.raising.modules.sys.service.ResourceService;
import com.raising.modules.sys.service.RoleService;
import com.raising.modules.sys.service.UserService;
import com.raising.utils.JSystemUtils;
import com.raising.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.List;
import java.util.Set;

/**
 * 首页控制器
 * @author gaoy
 */
@Controller
@RequestMapping(value = "${adminPath}")
public class AdminIndexController extends BaseController {

	@Autowired
	private ResourceService resourceService;
	@Autowired
	private UserService userService;
	@Autowired
	private RoleService roleService;
	/**
	 * 跳转到后台框架主页
	 * @author gaoy
	 * 2017 2017-5-9 上午10:20:41
	 * @param model
	 * @return
	 */
	@RequestMapping(method = RequestMethod.GET)
	public String index(Model model) {
		User user = UserUtils.getLoginUser();
		Set<String> permissions = userService.findPermissions(user.getRoleIds());
		List<Resource> menus = resourceService.findMenus(permissions);

		//做拼接
		List<Resource> menus_result = Lists.newArrayList();
		for(Resource resource:menus){
			//找出一级菜单
			if("1".equals(resource.getParentId())){
				//找出二级菜单
				List<Resource> resources2_list = Lists.newArrayList();
				for(Resource resource2:menus){
					if(resource.getId().equals(resource2.getParentId())){
						//找出三级菜单
						List<Resource> resources3_list = Lists.newArrayList();
						for(Resource resource3:menus){
							if(resource2.getId().equals(resource3.getParentId())){
								resources3_list.add(resource3);
							}
						}
						if( resources3_list!=null && resources3_list.size()>0 ){
							resource2.setChild(resources3_list);
						}
						resources2_list.add(resource2);
					}
				}
				if( resources2_list!=null && resources2_list.size()>0 ){
					resource.setChild(resources2_list);
				}
				menus_result.add(resource);
			}
		}
		model.addAttribute("menus",menus_result);
//		model.addAttribute("menus", menus);
		
		//默认页面展示路径
		List<String> roleList = UserUtils.getLoginUser().getRoleIds();
		String firstRole = roleList.get(0);
		//根据角色查询默认首页路径
		Role role = roleService.get(firstRole);
		String indexUrl = role.getIndexUrl();
		model.addAttribute("indexUrl", indexUrl);
		if(!"admin".equals(role.getSiteUrl())){
			return "redirect:"+role.getSiteUrl();
		}
		return "home";
	}
	/**
	 * 系统管理平台主页
	 * @author GaoYuan
	 * @datetime 2017/10/18 下午4:24
	 */
	@RequestMapping(value = "/welcome", method = RequestMethod.GET)
	public String welcome(Model model) throws Exception {
		//必须角色首页访问路径为welcome的才可以访问
		//默认页面展示路径
		List<String> roleList = UserUtils.getLoginUser().getRoleIds();
		String firstRole = roleList.get(0);
		//根据角色查询默认首页路径
		Role role = roleService.get(firstRole);
		String indexUrl = role.getIndexUrl();
		if(!"welcome".equals(indexUrl)){
			throw new Exception("没有权限访问");
		}
		//系统相关信息
		model.addAttribute("systemInfo", JSystemUtils.getSystemInfo());

		return "welcome";
	}
	

	

}
